package cn.iocoder.yudao.module.erp.dal.mysql.dp;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpwlmq.vo.DpWlmqPageReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Arrays;
import java.util.List;

/**
 * ERP乌鲁木齐制单 Mapper
 * @author 天使管理员
 */
@Mapper
public interface DpWlmqMapper extends BaseMapperX<DpWlmqDO> {

    default PageResult<DpWlmqDO> selectPage(DpWlmqPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DpWlmqDO>()
                .likeIfPresent(DpWlmqDO::getId, reqVO.getId())
                .likeIfPresent(DpWlmqDO::getNo, reqVO.getNo())
                .eqIfPresent(DpWlmqDO::getStoreType, reqVO.getStoreType())
                .eqIfPresent(DpWlmqDO::getInCount, reqVO.getInCount())
                .likeIfPresent(DpWlmqDO::getInName, reqVO.getInName())
                .eqIfPresent(DpWlmqDO::getInVolume, reqVO.getInVolume())
                .eqIfPresent(DpWlmqDO::getWeight, reqVO.getWeight())
                .eqIfPresent(DpWlmqDO::getDensity, reqVO.getDensity())
                .eqIfPresent(DpWlmqDO::getDeliveryPhone, reqVO.getDeliveryPhone())
                .likeIfPresent(DpWlmqDO::getDeliveryName, reqVO.getDeliveryName())
                .eqIfPresent(DpWlmqDO::getEarpayAmount, reqVO.getEarpayAmount())
                .eqIfPresent(DpWlmqDO::getInPic, reqVO.getInPic())
                .eqIfPresent(DpWlmqDO::getLine, reqVO.getLine())
                .eqIfPresent(DpWlmqDO::getFile, reqVO.getFile())
                .eqIfPresent(DpWlmqDO::getNote, reqVO.getNote())
                .likeIfPresent(DpWlmqDO::getShipper, reqVO.getShipper())
                .eqIfPresent(DpWlmqDO::getDestination, reqVO.getDestination())
                .eqIfPresent(DpWlmqDO::getConsignee, reqVO.getConsignee())
                .eqIfPresent(DpWlmqDO::getAdvancePayPack, reqVO.getAdvancePayPack())
                .eqIfPresent(DpWlmqDO::getAdvancePayPackUp, reqVO.getAdvancePayPackUp())
                .eqIfPresent(DpWlmqDO::getAdvancePayChinaFreight, reqVO.getAdvancePayChinaFreight())
                .eqIfPresent(DpWlmqDO::getAdvancePayForGoods, reqVO.getAdvancePayForGoods())
                .eqIfPresent(DpWlmqDO::getCollecting, reqVO.getCollecting())
                .eqIfPresent(DpWlmqDO::getCustomerRebate, reqVO.getCustomerRebate())
                .eqIfPresent(DpWlmqDO::getBusinessRebate, reqVO.getBusinessRebate())
                .eqIfPresent(DpWlmqDO::getStatus, reqVO.getStatus())
                .eqIfPresent(DpWlmqDO::getInventoryStatus, reqVO.getInventoryStatus())
                .eqIfPresent(DpWlmqDO::getIsSplit, reqVO.getIsSplit())
                .betweenIfPresent(DpWlmqDO::getCreateTime, reqVO.getCreateTime())
                .betweenIfPresent(DpWlmqDO::getChooseDate, reqVO.getChooseDateStr())
                .orderByDesc(DpWlmqDO::getId));
    }

    default PageResult<DpWlmqDO> getDpWlmqPage(DpWlmqPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DpWlmqDO>()
                .likeIfPresent(DpWlmqDO::getId, reqVO.getId())
                .inIfPresent(DpWlmqDO::getId,reqVO.getIds())
                .likeIfPresent(DpWlmqDO::getNo, reqVO.getNo())
                .betweenIfPresent(DpWlmqDO::getCreateTime, reqVO.getCreateTime())
                .betweenIfPresent(DpWlmqDO::getChooseDate, reqVO.getChooseDateStr())
                .eqIfPresent(DpWlmqDO::getCreator, reqVO.getCreator())
                .eqIfPresent(DpWlmqDO::getStatus, reqVO.getStatus())
                .eqIfPresent(DpWlmqDO::getLine, reqVO.getLine())
                .likeIfPresent(DpWlmqDO::getShipper, reqVO.getShipper())
                .inIfPresent(DpWlmqDO::getStatus,0,1)
                // 添加条件，查找 remCount 大于 0 的数据
                .gt(DpWlmqDO::getRemCount, 0)
                .orderByDesc(DpWlmqDO::getId));
    }

    default PageResult<DpWlmqDO> selectRemCountNoZeroPage(DpWlmqPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DpWlmqDO>()
                .likeIfPresent(DpWlmqDO::getId, reqVO.getId())
                .likeIfPresent(DpWlmqDO::getNo, reqVO.getNo())
                .betweenIfPresent(DpWlmqDO::getChooseDate, reqVO.getChooseDateStr())
                .eqIfPresent(DpWlmqDO::getCreator, reqVO.getCreator())
                .eqIfPresent(DpWlmqDO::getLine, reqVO.getLine())
                .eqIfPresent(DpWlmqDO::getStatus,1)
                //找出入库状态为0或2的
//                .in(DpWlmqDO::getInventoryStatus, Arrays.asList(0, 2))
                // 添加条件，查找 remCount 大于 0 的数据
                .gt(DpWlmqDO::getRemCount, 0)
                .orderByDesc(DpWlmqDO::getId));
    }

    @Select("<script>" +
            "SELECT temp.id FROM " +
            "(<foreach collection='ids' item='id' separator=' UNION ALL '>" +
            "SELECT #{id} AS id " +
            "</foreach>) temp " +
            "LEFT JOIN erp_dp_wlmq ON temp.id = erp_dp_wlmq.id " +
            "WHERE erp_dp_wlmq.id IS NULL" +
            "</script>")
    List<String> getNotExistIds(@Param("ids") List<String> ids);
}